package com.study.service.impl;

import com.study.model.MsgCode;
import com.study.service.MsgCodeService;
import com.study.utils.MsgCodeUtils;
import net.oschina.j2cache.CacheChannel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.concurrent.TimeUnit;

/**
 * @desc:
 * @author: panxiyi
 * @date: 2023/5/3 11:52 下午
 */

@Service
public class MsgCodeServiceImpl implements MsgCodeService {

    @Autowired
    private MsgCodeUtils msgCodeUtils;

    @Autowired
    private CacheChannel cacheChannel;


    @Override
    public String genCode(String tele) {
        String code = msgCodeUtils.generator(tele);
        cacheChannel.set("sms", tele, code);
        return code;
    }

    @Override
    public boolean checkCode(MsgCode msgCode) {
        String code = cacheChannel.get("sms", msgCode.getTele()).asString();
        return msgCode.getCode().equals(code);
    }
}
